//
//  GoodsListViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/7/5.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "GoodsListViewController.h"
#import "GoodsListBtn.h"
#import "HomepageCollectionViewCell.h"
#import "ScreenGoodsCollectionViewCell.h"
#import "GoodsDetailViewController.h"
#import "GoodsModel.h"

#define screenW rateWidth(80)
@interface GoodsListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITextFieldDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionView *screenCollectionView;
@property (nonatomic, strong) UIView *screenBgView;
@property (nonatomic, strong) UIView *blackV;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, strong) NSMutableArray *array1;
@property (nonatomic, strong) NSMutableArray *array2;
@property (nonatomic, strong) NSMutableArray *array3;
@property (nonatomic, strong) NSMutableArray *array4;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, assign) NSInteger selectIndex1;
@property (nonatomic, assign) NSInteger selectIndex2;
@property (nonatomic, assign) NSInteger selectIndex3;
@property (nonatomic, assign) NSInteger selectIndex4;
@property (nonatomic, strong) NSString *paiXuType; // 综合、销量、价格排序
@property (nonatomic, strong) GoodsListBtn *btn1;
@property (nonatomic, strong) GoodsListBtn *btn2;
@property (nonatomic, strong) GoodsListBtn *btn3;
@property (nonatomic, assign) BOOL isSelect; // 价格btn
@property (nonatomic, strong) NSString *fenLeiType; // 商品分类
@property (nonatomic, strong) NSString *pinPaiType; // 商品品牌
@property (nonatomic, strong) NSString *jiaGeType; // 价格区间
@property (nonatomic, strong) NSString *shuXingType; // 商品属性
@property (nonatomic, strong) UITextField *searchTF;

@end

@implementation GoodsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorWhite;
    [self loadBaseTitleLable:@""];
    [self setUpUI];
    
    self.currentPage = 1;
    self.pageSize = 20;
    
    self.selectIndex1 = 0;
    self.selectIndex2 = 0;
    self.selectIndex3 = 0;
    self.selectIndex4 = 0;
    self.paiXuType = @""; // 默认综合排序
    
    // 请求数据
    [self screenData];
    
    
    // 123
}
#pragma mark - 筛选
- (void)screenData
{
    NSArray *conditionArray = @[@"orderstatus=1",// 是否开启排序搜索，1 开启 0 关闭
                                @"orderlist=默认|default,上架|time,价格|price,销量|sales,评价|comment,点击|hits", // 指定排序方法，请参考示例格式，可以隐藏部分
                                @"classstatus=1", // 是否开启栏目搜索，1 开启 0 关闭
                                @"parentclassid=0", // 上级栏目ID，为0和为空调用一级栏目
                                @"classtitle=商品分类", // ---指定栏目搜索名称，不指定则返回默认值
                                @"specialstatus=1", // 是否开启专题搜索，1 开启 0 关闭
                                @"specialtitle=商品品牌", // ---指定专题搜索名称，不指定则返回默认值
                                @"fieldstatus=1", // 是否开启自定义字段筛选，1 开启 0 关闭
                                @"pricestatus=1", // 是否开启价格区间搜索，1 开启 0 关闭
                                @"pricetitle=价格区间",// ---指定价格区间搜索名称，不指定则返回默认值
                                @"pricelist=0-100,100-500,500-2000,2000-5000,5000-10000,10000-20000,20000-50000,50000以上", //指定价格区间搜索名称，不指定则返回默认值
                                @"attributestatus=1", // 是否开启属性搜索，1 开启 0 关闭
                                @"attributetitle=商品属性", // ---指定属性方法搜索名称，不指定则返回默认值
                                @"attributelist=推荐|elite,热门|hot,新品|xinpin,促销|cuxiao", // 指定属性方法，请参考示例格式，可以隐藏部分
                                ];
    [MBProgressHUD showMessage:nil toView:self.view];
    [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/content/content.ashx?action=searchselect&companyid=%@&channelid=105&%@",kServerURL,companyId,[conditionArray componentsJoinedByString:@"&"]] parameters:nil success:^(id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        
        if ([responseObject[@"status"] intValue] == 1) {
            self.titleArray = [NSArray array];
            self.array1 = [NSMutableArray array];
            self.array2 = [NSMutableArray array];
            self.array3 = [NSMutableArray array];
            self.array4 = [NSMutableArray array];
            self.titleArray = @[responseObject[@"classtitle"],responseObject[@"specialtitle"],responseObject[@"pricetitle"],responseObject[@"attributetitle"]];
            self.array1 = responseObject[@"classData"];
            self.array2 = responseObject[@"specialData"];
            self.array3 = responseObject[@"priceData"];
            self.array4 = responseObject[@"attributeData"];
            
            [self getSecondList];
        }
        [self.screenCollectionView reloadData];
        
    } failure:^(NSError *error) {
        [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
    }];
}
- (void)getSecondList
{
    if (!self.searchText) {
        self.searchText = @"";
    }
    [self setScreenItem];
    NSArray *array = @[[NSString stringWithFormat:@"order=%@",self.paiXuType],[NSString stringWithFormat:@"%@",self.fenLeiType],[NSString stringWithFormat:@"%@",self.pinPaiType],[NSString stringWithFormat:@"%@",self.jiaGeType],[NSString stringWithFormat:@"%@",self.shuXingType]];
    NSString *url = [NSString stringWithFormat:@"%@/api/json/content/content.ashx?action=list&companyid=%@&channelid=105&%@&pagesize=%@&pageindex=1&keyword=%@",kServerURL,companyId,[array componentsJoinedByString:@"&"],[NSString stringWithFormat:@"%ld",(long)self.pageSize],self.searchText];
    
    [MBProgressHUD showMessage:nil toView:self.view];
    [TFNetworking getWithURLString:url parameters:nil success:^(id responseObject) {
        [self.collectionView.mj_header endRefreshing];
        [MBProgressHUD hideHUDForView:self.view];
        self.dataArray = [NSMutableArray array];
        self.currentPage = 1;
        if ([responseObject[@"status"] intValue] == 1) {
            self.dataArray = [GoodsModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        }
        NSInteger i = [responseObject[@"count"] intValue]/self.pageSize;
        NSInteger j = [responseObject[@"count"] intValue]%self.pageSize;
        NSInteger n = 0;
        if (j > 0) {
            n = i+1;
        } else {
            n = i;
        }
        if (n<2) {
            self.collectionView.mj_footer.hidden = YES;
        } else {
            self.collectionView.mj_footer.hidden = NO;
        }
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
        [self.collectionView.mj_header endRefreshing];
    }];
}
- (void)getSecondListMore
{
    self.currentPage++;
    if (!self.searchText) {
        self.searchText = @"";
    }
    [self setScreenItem];
    NSArray *array = @[[NSString stringWithFormat:@"order=%@",self.paiXuType],[NSString stringWithFormat:@"%@",self.fenLeiType],[NSString stringWithFormat:@"%@",self.pinPaiType],[NSString stringWithFormat:@"%@",self.jiaGeType],[NSString stringWithFormat:@"%@",self.shuXingType]];
    NSString *url = [NSString stringWithFormat:@"%@/api/json/content/content.ashx?action=list&companyid=%@&channelid=105&%@&pagesize=%@&pageindex=%@&keyword=%@",kServerURL,companyId,[array componentsJoinedByString:@"&"],[NSString stringWithFormat:@"%ld",(long)self.pageSize],[NSString stringWithFormat:@"%ld",(long)self.currentPage],self.searchText];
    
    [MBProgressHUD showMessage:nil toView:self.view];
    [TFNetworking getWithURLString:url parameters:nil success:^(id responseObject) {
        [self.collectionView.mj_footer endRefreshing];
        [MBProgressHUD hideHUDForView:self.view];
        if ([responseObject[@"status"] intValue] == 1) {
            
            NSInteger i = [responseObject[@"count"] intValue]/self.pageSize;
            NSInteger j = [responseObject[@"count"] intValue]%self.pageSize;
            NSInteger n = 0;
            if (j > 0) {
                n = i+1;
            } else {
                n = i;
            }
            if (self.currentPage > n) {
                self.currentPage--;
                self.collectionView.mj_footer.hidden = YES;
            } else {
                self.collectionView.mj_footer.hidden = NO;
                self.dataArray = (NSMutableArray *)[self.dataArray arrayByAddingObjectsFromArray:[GoodsModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]]];
                [self.collectionView reloadData];
            }
        } else {
            self.currentPage--;
            self.collectionView.mj_footer.hidden = YES;
        }
        
    } failure:^(NSError *error) {
        [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
        self.currentPage--;
        [self.collectionView.mj_footer endRefreshing];
    }];
}
#pragma mark - 设置筛选参数
- (void)setScreenItem
{
    // 1、综合 销量 价格 order self.paiXuType
    // 排序方式 1 上架时间正序 2 上架时间倒序 3 价格从低到高 4 价格从高到低 5 销量从低到高 6 销量从高到低 7 评价从低到高 8 评价从高到低 9 浏览次数从低到高 10 浏览次数从高到低
    // 2、筛选 -- 商品分类 classid 栏目ID
    //    筛选 -- 商品品牌 specialid 专题ID
    //    筛选 -- 价格区间 price1 最低价格 price2 最高价格
    //    筛选 -- 商品属性 attribute
    // 1、栏目ID
    if (self.classid) {
        self.fenLeiType = [NSString stringWithFormat:@"classid=%@",self.classid];
    } else {
        NSDictionary *dic = self.array1[self.selectIndex1];
        self.fenLeiType = dic[@"where"];
    }
    // 2、专题ID
    NSDictionary *dic1 = self.array2[self.selectIndex2];
    self.pinPaiType = dic1[@"where"];
    // 3、价格区间
    NSDictionary *dic2 = self.array3[self.selectIndex3];
    self.jiaGeType = dic2[@"where"];
    // 4、商品属性
    if (self.type == 1) {
        // 推荐商品
        self.shuXingType = @"elite=1";
    } else if (self.type == 2) {
        // 新品上架
        self.shuXingType = @"xinpin=1";
    } else {
        NSDictionary *dic = self.array4[self.selectIndex4];
        self.shuXingType = dic[@"where"];
    }
}
- (void)setUpUI
{
    // 搜索框
    UIImageView *searchV = [[UIImageView alloc] init];
    searchV.image = UIImageNamed(@"goodsListSearch");
    [searchV sizeToFit];
    [self.navTitleBgView addSubview:searchV];
    [searchV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.navTitleBgView);
        make.centerY.equalTo(self.leftBtn);
    }];
    searchV.userInteractionEnabled = YES;
    UITextField *searchTF = [[UITextField alloc] init];
    searchTF.returnKeyType = UIReturnKeySearch;
    searchTF.textColor = UIColorFromRGB(0x333333);
    searchTF.font = UIFontSystem(13);
    [searchV addSubview:searchTF];
    [searchTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(rateWidth(400), rateHeight(45)));
        make.left.equalTo(searchV).offset(rateWidth(70));
        make.centerY.equalTo(searchV);
    }];
    searchTF.delegate = self;
    self.searchTF = searchTF;
    self.searchTF.text = self.searchText;
    
    // 筛选view
    UIView *topV = [[UIView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth, rateHeight(90))];
    topV.backgroundColor = UIColorFromRGB(0xf5f5f5);
    [self.view addSubview:topV];
    
    // 综合
    GoodsListBtn *btn1 = [[GoodsListBtn alloc] init];
    btn1.rightImg.image = UIImageNamed(@"arrow_down");
    btn1.textLB.text = @"综合";
    [topV addSubview:btn1];
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topV);
        make.centerY.equalTo(topV);
        make.size.mas_offset(CGSizeMake(kScreenWidth/4, rateHeight(90)));
    }];
    btn1.tag = 1;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [btn1 addGestureRecognizer:tap1];
    
    // 销量
    GoodsListBtn *btn2 = [[GoodsListBtn alloc] init];
    btn2.rightImg.image = UIImageNamed(@"arrow_down_grey");
    btn2.textLB.text = @"销量";
    [topV addSubview:btn2];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(topV).offset(kScreenWidth/4);
        make.centerY.equalTo(topV);
        make.size.mas_offset(CGSizeMake(kScreenWidth/4, rateHeight(90)));
    }];
    btn2.tag = 2;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [btn2 addGestureRecognizer:tap2];
    
    // 价格
    GoodsListBtn *btn3 = [[GoodsListBtn alloc] init];
    btn3.textLB.text = @"价格";
    btn3.rightImg.image = UIImageNamed(@"price_default"); // price_up price_down
    [topV addSubview:btn3];
    [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topV).offset(-kScreenWidth/4);
        make.centerY.equalTo(topV);
        make.size.mas_offset(CGSizeMake(kScreenWidth/4, rateHeight(90)));
    }];
    btn3.tag = 3;
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [btn3 addGestureRecognizer:tap3];
    self.btn1 = btn1;
    self.btn2 = btn2;
    self.btn3 = btn3;
    
    // 筛选
    GoodsListBtn *btn4 = [[GoodsListBtn alloc] init];
    btn4.textLB.text = @"筛选";
    btn4.rightImg.image = UIImageNamed(@"goodsshaixuan");
    [topV addSubview:btn4];
    [btn4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(topV);
        make.centerY.equalTo(topV);
        make.size.mas_offset(CGSizeMake(kScreenWidth/4, rateHeight(90)));
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionScreen)];
    [btn4 addGestureRecognizer:tap];
    // 商品列表
    [self.view addSubview:self.collectionView];
    
    // 筛选view
    [self addScreenView];
}
#pragma mark - 搜索
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    self.searchText = _searchTF.text;
    [self getSecondList];
    // 点击搜索按钮回收键盘
    [_searchTF resignFirstResponder];
    return YES;
}
#pragma mark - 综合排序
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    GoodsListBtn *btn = (GoodsListBtn *)[tap view];
    if (btn.tag == 1) {
        // 综合
        self.paiXuType = @"";
        self.btn1.rightImg.image = UIImageNamed(@"arrow_down");
        self.btn2.rightImg.image = UIImageNamed(@"arrow_down_grey");
        self.btn3.rightImg.image = UIImageNamed(@"price_default");
        self.isSelect = NO;
        
    } else if (btn.tag == 2) {
        // 销量
        self.paiXuType = @"6";
        self.btn1.rightImg.image = UIImageNamed(@"arrow_down_grey");
        self.btn2.rightImg.image = UIImageNamed(@"arrow_down");
        self.btn3.rightImg.image = UIImageNamed(@"price_default");
        self.isSelect = NO;
        
    } else if (btn.tag == 3) {
        // 价格
        self.isSelect = !self.isSelect;
        if (self.isSelect) {
            // 低-高
            self.paiXuType = @"3";
            self.btn1.rightImg.image = UIImageNamed(@"arrow_down_grey");
            self.btn2.rightImg.image = UIImageNamed(@"arrow_down_grey");
            self.btn3.rightImg.image = UIImageNamed(@"price_up");
        } else {
            // 高-低
            self.paiXuType = @"4";
            self.btn1.rightImg.image = UIImageNamed(@"arrow_down_grey");
            self.btn2.rightImg.image = UIImageNamed(@"arrow_down_grey");
            self.btn3.rightImg.image = UIImageNamed(@"price_down");
        }
    }
    [self getSecondList];
}
#pragma mark - 添加筛选view
- (void)addScreenView
{
    WeakSelf
    self.blackV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _blackV.backgroundColor = [UIColorBlack colorWithAlphaComponent:0.7];
    [self.view addSubview:_blackV];
    _blackV.hidden = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeAction)];
    [_blackV addGestureRecognizer:tap];
    // 筛选view
    self.screenBgView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth-screenW, kScreenHeight)];
    self.screenBgView.backgroundColor = UIColorWhite;
    [self.view addSubview:_screenBgView];
    // btn
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setImage:UIImageNamed(@"close") forState:(UIControlStateNormal)];
    [btn sizeToFit];
    [_screenBgView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.screenBgView).offset(TopHeight+5);
        make.left.equalTo(weakSelf.screenBgView).offset(rateWidth(20));
    }];
    [btn addTarget:self action:@selector(closeAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    UILabel *label = [UILabel labelWithText:@"快速筛选" textColor:UIColorFromRGB(0x333333) fontSize:16];
    [label sizeToFit];
    [_screenBgView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.screenBgView);
        make.centerY.equalTo(btn);
    }];
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = globalGray;
    [_screenBgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.screenBgView);
        make.bottom.equalTo(weakSelf.screenBgView.mas_top).offset(SafeAreaTopHeight);
        make.height.equalTo(@(rateHeight(2)));
    }];
    
    // 筛选列表
    [_screenBgView addSubview:self.screenCollectionView];
    // 重置确定按钮
    UIButton *btn1 = [UIButton buttonWithTitle:@"重置" fontSize:15 titleColor:UIColorFromRGB(0x333333) background:globalGray cornerRadius:0];
    [_screenBgView addSubview:btn1];
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.screenBgView);
        make.bottom.equalTo(weakSelf.screenBgView);
        make.size.mas_offset(CGSizeMake((kScreenWidth-screenW)/2, rateHeight(90)));
    }];
    [btn1 addActionHandler:^{
        self.selectIndex1 = 0;
        self.selectIndex2 = 0;
        self.selectIndex3 = 0;
        self.selectIndex4 = 0;
        [self.screenCollectionView reloadData];
    }];
    
    UIButton *btn2 = [UIButton buttonWithTitle:@"确定" fontSize:15 titleColor:UIColorWhite background:navBarColor cornerRadius:0];
    [_screenBgView addSubview:btn2];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.screenBgView);
        make.bottom.equalTo(weakSelf.screenBgView);
        make.size.mas_offset(CGSizeMake((kScreenWidth-screenW)/2, rateHeight(90)));
    }];
    [btn2 addActionHandler:^{
        self.classid = nil;
        self.type = 100;
        [self closeAction];
        [self getSecondList];
    }];
}
#pragma mark - 筛选
- (void)actionScreen
{
    [UIView animateWithDuration:0.4 animations:^{
        self.screenBgView.frame = CGRectMake(screenW, 0, kScreenWidth-screenW, kScreenHeight);
    }];
    _blackV.hidden = NO;
}
- (void)closeAction
{
    [UIView animateWithDuration:0.3 animations:^{
        self.screenBgView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth-screenW, kScreenHeight);
    }];
    _blackV.hidden = YES;
}

#pragma mark - collectionview delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (collectionView == _screenCollectionView) {
        return 4;
    } else {
        return 1;
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _collectionView) {
        return _dataArray.count;
    } else {
        if (section == 0) {
            return self.array1.count;
        } else if (section == 1) {
            return self.array2.count;
        } else if (section == 2) {
            return self.array3.count;
        } else {
            return self.array4.count;
        }
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _collectionView) {
        HomepageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
        cell.goodsTitle.font = UIFontSystem(13);
        cell.goodsTitle.textColor = UIColorFromRGB(0x909090);
        if (_dataArray.count != 0) {
            GoodsModel *model = _dataArray[indexPath.row];
            [cell.goodsImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:%@",model.images]]];
            cell.goodsTitle.text = model.title;
            cell.priceLB.text = [NSString stringWithFormat:@"￥%.2f",[model.price floatValue]];
            cell.collectionNumLB.text = model.hits;
        }
        return cell;
    } else {
        ScreenGoodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"screenCollectionViewCell" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            if (self.selectIndex1 == indexPath.row) {
                cell.textLB.textColor = UIColorWithRGB(81, 164, 92);
                cell.textLB.layer.borderColor = UIColorWithRGB(81, 164, 92).CGColor;
            } else {
                cell.textLB.textColor = UIColorFromRGB(0x333333);
                cell.textLB.layer.borderColor = globalGray.CGColor;
            }
            NSDictionary *dic = self.array1[indexPath.row];
            cell.textLB.text = dic[@"name"];
        } else if (indexPath.section == 1) {
            if (self.selectIndex2 == indexPath.row) {
                cell.textLB.textColor = UIColorWithRGB(81, 164, 92);
                cell.textLB.layer.borderColor = UIColorWithRGB(81, 164, 92).CGColor;
            } else {
                cell.textLB.textColor = UIColorFromRGB(0x333333);
                cell.textLB.layer.borderColor = globalGray.CGColor;
            }
            NSDictionary *dic = self.array2[indexPath.row];
            cell.textLB.text = dic[@"name"];
        } else if (indexPath.section == 2) {
            if (self.selectIndex3 == indexPath.row) {
                cell.textLB.textColor = UIColorWithRGB(81, 164, 92);
                cell.textLB.layer.borderColor = UIColorWithRGB(81, 164, 92).CGColor;
            } else {
                cell.textLB.textColor = UIColorFromRGB(0x333333);
                cell.textLB.layer.borderColor = globalGray.CGColor;
            }
            NSDictionary *dic = self.array3[indexPath.row];
            cell.textLB.text = dic[@"name"];
        } else if (indexPath.section == 3) {
            if (self.selectIndex4 == indexPath.row) {
                cell.textLB.textColor = UIColorWithRGB(81, 164, 92);
                cell.textLB.layer.borderColor = UIColorWithRGB(81, 164, 92).CGColor;
            } else {
                cell.textLB.textColor = UIColorFromRGB(0x333333);
                cell.textLB.layer.borderColor = globalGray.CGColor;
            }
            NSDictionary *dic = self.array4[indexPath.row];
            cell.textLB.text = dic[@"name"];
        }
        return cell;
    }
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _collectionView) {
        return CGSizeMake((kScreenWidth-rateWidth(60)-rateWidth(30))/2, rateWidth(440));
    } else {
        return CGSizeMake((kScreenWidth-screenW-rateWidth(60))/3, rateHeight(60));
    }
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (collectionView == _collectionView) {
        return UIEdgeInsetsMake(0, rateWidth(30), 0, rateWidth(30));//分别为上、左、下、右
    } else {
        return UIEdgeInsetsMake(rateHeight(10), rateWidth(10), rateHeight(10), rateWidth(10));//分别为上、左、下、右
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _screenCollectionView) {
        if (indexPath.section == 0) {
            self.selectIndex1 = indexPath.row;
        } else if (indexPath.section == 1) {
            self.selectIndex2 = indexPath.row;
        } else if (indexPath.section == 2) {
            self.selectIndex3 = indexPath.row;
        } else if (indexPath.section == 3) {
            self.selectIndex4 = indexPath.row;
        }
        [self.screenCollectionView reloadData];
        
    } else {
        if (_dataArray.count != 0) {
            GoodsModel *model =_dataArray[indexPath.row];
            GoodsDetailViewController *goodsD = [[GoodsDetailViewController alloc] init];
            goodsD.contentId = model.id;
            [self.navigationController pushViewController:goodsD animated:YES];
        }
    }
}
//要先设置表头高度
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (collectionView == _screenCollectionView) {
        CGSize size = {kScreenWidth, 40};
        return size;
    } else {
        CGSize size = {0, 0};
        return size;
    }
}
//在表头内添加内容,需要创建一个继承collectionReusableView的类,用法类比tableViewcell
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _screenCollectionView) {
        // 注册表头
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
        
        // 初始化表头
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        UIView * contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-screenW, 40)];
        contentView.backgroundColor = UIColorWhite;
        UILabel * label = [UILabel labelWithText:self.titleArray[indexPath.section] textColor:UIColorFromRGB(0x333333) fontSize:15];
        [label sizeToFit];
        [contentView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(contentView).offset(rateWidth(15));
            make.centerY.equalTo(contentView);
        }];
        
        [headerView addSubview:contentView];
        
        return headerView;
    } else {
        return nil;
    }
}
- (UICollectionView *)screenCollectionView
{
    if (!_screenCollectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = rateHeight(10);
        layout.minimumInteritemSpacing = rateWidth(15);
        _screenCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth-screenW, kScreenHeight-SafeAreaTopHeight-rateHeight(90)) collectionViewLayout:layout];
        _screenCollectionView.dataSource = self;
        _screenCollectionView.delegate = self;
        _screenCollectionView.backgroundColor = UIColorWhite;
        [_screenCollectionView registerClass:[ScreenGoodsCollectionViewCell class] forCellWithReuseIdentifier:@"screenCollectionViewCell"];
        _screenCollectionView.showsVerticalScrollIndicator = NO;
    }
    return _screenCollectionView;
}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight+rateHeight(90), kScreenWidth, kScreenHeight-SafeAreaTopHeight-rateHeight(90)) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = UIColorWhite;
        [_collectionView registerClass:[HomepageCollectionViewCell class] forCellWithReuseIdentifier:@"collectionViewCell"];
        _collectionView.showsVerticalScrollIndicator = NO;
        WeakSelf
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf getSecondList];
        }];
        _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getSecondListMore];
        }];
    }
    return _collectionView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
